
import '../event/set_eas_event.dart';
import '../main.dart';
import 'socket.dart';

enum Mode { reader, identify, inventoryHfRfid,borrowBook,returnBook,none }


class SocketManager{

  late SocketManager manager;

  SocketManager getManager(){
    return manager;
  }

  factory SocketManager() => _singleton;

  static final SocketManager _singleton = SocketManager._internal();

  SocketManager._internal();

  Mode? mode;
  // String? uid;
  // String? no;
  bool? isBorrow;

  setMode(Mode mode){
    this.mode = mode;
  }

  setModeAndStart(Mode mode,{String uid = '', String no = '', bool isBorrow = true}){
    this.mode = mode;
    // this.uid = uid;
    // this.no = no;
    this.isBorrow = isBorrow;
    if(mode == Mode.reader){
      socketHelper.add(m1Inventory());
    }
    if(mode == Mode.identify){
      socketHelper.add(icrInventory());
    }
    if(mode == Mode.inventoryHfRfid){
      socketHelper.add(inventoryHfRfid());
    }
    if(mode == Mode.borrowBook || mode == Mode.returnBook){
      socketHelper.add(setEas(uid,no, isBorrow));
    }
  }

  start(){
    if(mode == Mode.reader){
      socketHelper.add(m1Inventory());
    }
    if(mode == Mode.identify){
      socketHelper.add(icrInventory());
    }
    if(mode == Mode.inventoryHfRfid){
      socketHelper.add(inventoryHfRfid());
    }
  }

  afi(uid,no){
    socketHelper.add(setAfi(uid,no, isBorrow==true?0xC2:0x07));
  }

  success(uid,no){
    eventFactory.fire(SetEasEvent(true,uid,no));
  }
  fail(uid,no){
    eventFactory.fire(SetEasEvent(false,uid,no));
  }



}